package com.dominator.controller.api;

import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.utils.JsonUtils;
import com.dominator.enums.ReqEnums;
import com.dominator.service.ApiLoginService;
import com.dominator.service.ApiUserService;
import com.dominator.utils.api.ApiMessage;
import com.dominator.utils.exception.ApiException;
import com.dominator.utils.network.IpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@CrossOrigin
@RestController
@RequestMapping("api/v1/login")
@Slf4j
public class ApiLoginController {


    @Autowired
    HttpServletRequest request;


    @Autowired
    ApiLoginService apiLoginService;


    @Autowired
    ApiUserService apiUserService;

    /**
     * 注册用户
     *
     * @param
     * @return
     */
    @PostMapping(value = "/register")
    public ApiMessage register(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiLoginService.register(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }
    }

    /**
     * 图形验证码注册
     *
     * @param
     * @return
     */
    @PostMapping(value = "/picRegister")
    public ApiMessage picRegister(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiLoginService.picRegister(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }
    }


    /**
     * 手机密码登录
     *
     * @param
     * @return
     */
    @PostMapping(value = "/phonePasswordLogin")
    public ApiMessage phonePasswordLogin(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiLoginService.phonePasswordLogin(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }
    }

    /**
     * 手机验证码登录
     *
     * @param
     * @return
     */
    @PostMapping(value = "/phoneVerifyLogin")
    public ApiMessage phoneVerifyLogin(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiLoginService.phoneVerifyLogin(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }
    }



    /**
     * openId登录
     *
     * @param
     * @return
     */
    @PostMapping(value = "/wxOpenIdLogin")
    public ApiMessage wxOpenIdLogin(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiLoginService.wxOpenIdLogin(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }
    }


    /**
     * 湖州驴头登录
     *
     * @param
     * @return
     */
    @PostMapping(value = "/hzLogin")
    public ApiMessage hzLogin(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiLoginService.hzLogin(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }
    }







    /**
     * 找回密码
     *
     * @param
     * @return
     */
    @PostMapping(value = "/findPassword")
    public ApiMessage findPassword(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiUserService.findPassword(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }
    }



    /**
     * 刮刮卡登录
     *
     * @param
     * @return
     */
    @PostMapping(value = "/guaLogin")
    public ApiMessage guaLogin(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiLoginService.guaLogin(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }
    }


    /**
     * 微信openId登录或注册
     *
     * @param
     * @return
     */
    @PostMapping(value = "/wxOpenIdLoginOrRegister")
    public ApiMessage wxOpenIdLoginOrRegister(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiLoginService.wxOpenIdLoginOrRegister(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }
    }


    /**
     * 绑定手机号
     *
     * @param
     * @return
     */
    @PostMapping(value = "/bindPhone")
    public ApiMessage bindPhone(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiLoginService.bindPhone(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }
    }



    /**
     * openid获取用户信息
     */
    @RequestMapping("/getUserByOpenId")
    @ResponseBody
    public ApiMessage getUserByOpenId(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiUserService.getUserByOpenId(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }
    }

    /**
     * 修改身份信息
     */
    @RequestMapping("/editRealName")
    @ResponseBody
    public ApiMessage editRealName(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiUserService.editRealName(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }
    }


    /**
     * 获取用户游戏机登录信息
     */
    @RequestMapping("/getMachineLoginLog")
    @ResponseBody
    public ApiMessage getMachineLoginLog(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiLoginService.getMachineLoginLog(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }
    }





    /**
     * 用户名登录
     */
    @PostMapping("/userNameLogin")
    public ApiMessage userNameLogin(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiLoginService.userNameLogin(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }
    }


    /**
     * 邮箱注册
     */
    @PostMapping("/emailRegister")
    public ApiMessage emailRegister(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiLoginService.emailRegister(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }
    }







}
